/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package wwcontrol;

import com.phidgets.*;
import com.phidgets.event.*;
//import java.util.Timer;
import java.util.TimerTask;
import java.util.Date;

/**
 *
 * @author Michael Ferry
 */
public class HeaterControlTask extends TimerTask{
    private InterfaceKitPhidget HeaterSensorIFK;
    private InterfaceKitPhidget RelayOutputIFK;
    //Contains two arrays of equal length. The first array is an array of sensor 
    //positions in the HeaterSensorIFK that contains the sensor index
    //The second array contains a mapping of the digital ouputs for the relay
    //controlling that barrel's heater.
    //private int[] SensorOutputMap; 
    private double TempSetPoint;
    private double TempSetRange;
    private int CurrentRawSensor;
    private double CurrentBarrelTemperature;
    
    public HeaterControlTask(InterfaceKitPhidget HeaterSensorIFK,InterfaceKitPhidget RelayOutputIFK,
            double TempSetPoint, double TempSetRange){
        this.HeaterSensorIFK=HeaterSensorIFK;
        this.RelayOutputIFK=RelayOutputIFK;
        //this.SensorOutputMap=SensorOutputMap;
        this.TempSetPoint=TempSetPoint;
        this.TempSetRange=TempSetRange;
    }
            
    @Override
    public void run() {
        //Go through heater's and determine which one need their states changed
        for (int i = 0; i < WWcontrolUI.HeaterSensorMap.length; i++) {
            //System.out.println(SensorOutputMap.length);
            try {
                if (HeaterSensorIFK.isAttached() && RelayOutputIFK.isAttached()) {
                    CurrentRawSensor = HeaterSensorIFK.getSensorRawValue(i);
                    CurrentBarrelTemperature = SensorComputations.ComputeTemperature2252ohms(CurrentRawSensor);
                    //If the barrel temperature is below the TempSet point minus the TempSetRange turn the heater on
                    //System.out.println("CurrTemp, barrel " + i +" "+ CurrentBarrelTemperature + " TempSet " + TempSetPoint + " TempSetRange" + TempSetRange);
                    if (CurrentBarrelTemperature < (TempSetPoint - TempSetRange)) {
                        //System.out.println("Turning on heater: "+ SensorOutputMap[i]);
                        RelayOutputIFK.setOutputState(WWcontrolUI.HeaterSensorMap[i], true);
                    } //If the barrel temperature is above the TempSet point minus the TempSetRange turn the heater off
                    else if (CurrentBarrelTemperature > TempSetPoint + TempSetRange) {
                        //System.out.println("Turning off heater: "+ SensorOutputMap[i]);
                        RelayOutputIFK.setOutputState(WWcontrolUI.HeaterSensorMap[i], false);
                    } else {
                        //System.out.println("No change in heater state");
                    }
                } else {
                    Date curTime = new Date();
                    System.out.println("Heater component(s) not attached at :"+curTime);
                }
                //Note if the barrel temperature is between TempSetPoint - TempSetRange and TempSetPoint + TempSetRange
                //No action will be taken.  This is a simple hystersis to prevent the heaters from being continually 
                //switched on and off

            } catch (PhidgetException ex) {
                System.out.println(ex);
                //JOptionPane.showMessageDialog(this, ex.getDescription(), "Phidget Error" + ex.getErrorNumber(), JOptionPane.ERROR_MESSAGE);
                //dispose();
                //System.exit(0);
            }
        }
    }
    
    
    
    
}
